import request from '@/utils/request'

export function getDictByType(type) {
  const result = {}
  const residueType = getStaticDict(result, type)
  if (residueType === '') {
    return result
  } else {
    return request({
      url: '/arpa-basic-api/dict/listDict',
      method: 'get',
      params: { types: residueType }
    }).then(res => {
      if (res.status === 0) {
        const list = res.data
        if (list && list.length > 0) {
          for (const i in list) {
            const dictMap = {}
            result[list[i].type] = dictMap
            const dicts = list[i].dictList
            if (dicts && dicts.length > 0) {
              for (const j in dicts) {
                dictMap[dicts[j].code] = dicts[j].name
              }
            }
          }
        }
        return result
      }
    })
  }
}
function getStaticDict(result, type) {
  let residueType = ''
  const dict = {
    isRelease: { '0': '未发布', '1': '已发布' },
    newsType: { '1': '最新活动', '2': '行业动态', '3': '公司新闻', '4': '节日安排', '5': '企业风采' }
  }
  if (type.indexOf(',') > -1) {
    const types = type.split(',')
    for (const i in types) {
      if (dict[types[i]]) {
        result[types[i]] = dict[types[i]]
      } else {
        if (residueType !== '') {
          residueType += ','
        }
        residueType += types[i]
      }
    }
  } else {
    if (dict[type]) {
      result[type] = dict[type]
    } else {
      residueType += type
    }
  }
  return residueType
}

// 获取 数据字典
export function getListDict(query) {
  return request({
    url: `/arpa-basic-api/dict/listDict`,
    method: 'get',
    params: query
  })
}
